package controllers;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.mail.EmailException;

import models.*;
import play.mvc.Controller;
import play.mvc.With;

//@With(Secure.class)						////this class is used for the login, Doaa El-Hawary
public class Roles extends Controller { // Created by Tarek Sheasha

	/**
	 * Assigns a role for a user in an organization
	 * @author		Rania Nader Saeed
	 * @version 	2011.04.29
	 * @category	C1S4
	 * @param		User to be assigned a role
	 * 				Organization in which a new role will be assigned to user
	 * 				Role to be assigned to user in the organization
	 * @return		void
	 * @throws 		EmailException 
	 */
	 
	public static void assignRoleInOrg(long userID, long orgID, String newRole) throws EmailException
	{
		User user = User.findById(userID);
		Organization org = Organization.findById(orgID);

		Role role = new Role(org, newRole);
		user.getRoleList().add(role);
		String content = "Congratulations " + user.getfName()
				+ "\n You are now a/an " + newRole + " in the Organization "
				+ org.getName();
		user.sendNotification(user, "New Role in an Organization!", content);
		user.save();
		
		List users = User.findAll();
		List organizations = Organization.findAll();
		render(users, organizations);
	}
	
	/**
	 * Assigns a role for a user in a Tag
	 * @author		Rania Nader Saeed
	 * @version 	2011.04.29
	 * @category	C1S4
	 * @param		User to be assigned a role
	 * 				Tag in which a new role will be assigned to user
	 * 				Role to be assigned to user in the tag
	 * @return		void
	 * @throws 		EmailException 
	 */
	public static void assignRoleInTag(long userID, long tagID, String newRole) throws EmailException
	{
		User user	= User.findById(userID);
		Tag tag		= Tag.findById(tagID);

		Role role = new Role(tag, newRole);
		user.getRoleList().add(role);
		String content = "Congratulations " + user.getfName()
		+ "\n You are now a/an " + newRole + " in the Tag "
		+ tag.getName() + ".";
		user.sendNotification(user, "New Role in a Tag!", content);
		user.save();
		
		List users = User.findAll();
		List tags = Tag.findAll();
		render(users, tags);
	}
	
	/**
	 * Assigns a role for a user in an Entity
	 * @author		Rania Nader Saeed
	 * @version 	2011.04.29
	 * @category	C1S4
	 * @param		User to be assigned a role
	 * 				Entity in which a new role will be assigned to user
	 * 				Role to be assigned to user in the entity
	 * @return		void
	 * @throws 		EmailException 
	 */
	public static void assignRoleInEntity(long userID, long entityID, String newRole) throws EmailException
	{
		User user		= User.findById(userID);
		Eentity entity	= Eentity.findById(entityID);
		
		Role role = new Role(entity, newRole);
		user.getRoleList().add(role);
		String content = "Congratulations " + user.getfName()
		+ "\n You are now a/an " + newRole + " in the entity "
		+ entity.getName() + ".";
		user.sendNotification(user, "New Role in a Tag!", content);
		user.save();
		
		List users	= User.findAll();
		List ent	= Eentity.findAll();
		render(users, ent);
	}
	

	/**
	 * Assigns a role for a user in a topic
	 * @author		Rania Nader Saeed
	 * @version 	2011.04.29
	 * @category	C1S4
	 * @param		User to be assigned a role
	 * 				Topic in which a new role will be assigned to user
	 * 				Role to be assigned to user in the entity
	 * @return		void
	 * @throws 		EmailException 
	 */
	public static void assignRoleInTopic(long userID, long topicID, String newRole) throws EmailException
	{
		User user		= User.findById(userID);
		Topic topic		= Topic.findById(topicID);
		
		Role role = new Role(topic, newRole);
		user.getRoleList().add(role);
		String content = "Congratulations " + user.getfName()
		+ "\n You are now a/an " + newRole + " in the topic "
		+ topic.getTitle() + ".";
		user.sendNotification(user, "New Role in a Tag!", content);
		user.save();
		
		List users	= User.findAll();
		List topics	= Topic.findAll();
		render(users, topics);
	}
	
	/**
	 * editRoleInTopic() is responsible for editing the permissions of an organizer inside a topic  
	 * @author 		Nirmeen Sallam
	 * @version 	2011.0501
	 * @category 	C1S11
	 * 
	 * @params      Long organizationId     :The id of the Organization of the topic
	 *              
	 *              Long  topicId           :The Id of the required topic
	 * 				
	 *              Long organiserId        :The Id of the required organizer
	 * These are the actions(permissions) that the organizer can have: 				
	 *              boolean canEdit					
	 * 				boolean canPost
	 * 				boolean canDelete
	 * 				boolean canInvite
	 * 				boolean canAdministerTopic
	 * 				boolean canBeRestricted
	 * 
	 * @return		void 
	 */
	
	
	public static  void editRoleInTopic(long organizationId ,long topicId, long organiserId,
			boolean canEdit, boolean canPost, boolean canDelete, boolean canInvite,
			boolean canAdministerTopic, boolean canBeRestricted) {
        
		String result = "";
		User organizationLead = User.find("byUsername", Security.connected()).first();
		
		Organization organization = Organization.findById(organizationId);
		
		User organiser = User.findById(organiserId);
		
		Topic topic = Topic.findById(topicId);
		
		String roleOfUser = organizationLead.getUserType(organization);
		System.out.println(roleOfUser +"my role");
		System.out.println(organization.getAllTopics().contains(topic));
		if (roleOfUser.equalsIgnoreCase("Lead") && organization.getAllTopics().contains(topic)) {
		
			for (int i = 0; i < organiser.getRoleList().size(); i++) {
			
				Role tmpRole = organiser.getRoleList().get(i);
			
			
			
				if(tmpRole.getRelatedTopic().equals(topic) && tmpRole.getUser().equals(organiser) && tmpRole.getTitle().equals("Organiser")){
				
					tmpRole.setCanEdit(canEdit);
				
					tmpRole.setCanPost(canPost);
				
					tmpRole.setCanDelete(canDelete);
				
					tmpRole.setCanInvite(canInvite);
				
					tmpRole.setCanAdministerTopic(canAdministerTopic);
				
					tmpRole.setCanBeRestricted(canBeRestricted);
					result= "You have edited the role of the organizer  Successfully =)";
					
		
					editRoleInTopicResult(result);
			}
		
			}
			result= "Faliure 1:editing the role of the organizer failed please make sure to enter the info. correctly";
           editRoleInTopicResult(result);
		}
        result="Failure 2:editing the role of the organizer failed ,you are not authorized to do such an action";
        editRoleInTopicResult(result);
	}
	
	/**
	 * This methods render to a view that has the Success or Failure message of editing the role of an organizer in a topic action
	 * 
	 * @author Nirmeen Sallam
	 * @version 2011.0506
	 * @category C1S11
	 * @param String result : message that have the success or failure of the action
	 * @return void
	 */
	public static void editRoleInTopicResult(String result){
		render(result);
	}
	
	/**
	 * This methods render to a view that has the Form of editing the role of an organizer in a topic
	 * 
	 * @author Nirmeen Sallam
	 * @version 2011.0506
	 * @category C1S11
	 * @param none
	 * @return void
	 */
	public static void editRoleInTopicButton(){
	
		render();
	
	}
	/**
	 * editRoleInEntity is supposed to edit an ORGANIZER permissions inside an ENTITY
	 * 
	 * @author Hossam Saraya
	 * @version May 3, 2011
	 * @category C1S10
	 * @params		Entity 	entity: 					The required entity
	 * 				User 	Organizer:					The required Organizer
	 * 				boolean canEdit					
	 * 				boolean canPost
	 * 				boolean canDelete
	 * 				boolean canCreateOrganisation
	 * 				boolean canInvite
	 * 				boolean canAdministerTopic
	 * 				boolean canBeRestricted
	 * 				boolean canAdministerEntity
	 * 
	 * @renders		status message containing operation status
	 */
	public static void editRoleInEntity(Long entityId,Long userId,boolean canEdit, boolean canPost, boolean canDelete,
			boolean canCreateOrganisation, boolean canInvite,
			boolean canAdministerTopic, boolean canBeRestricted,
			boolean canAdministerEntity){
		String statusMessage="";
		try{
			User user = User.findById(userId);
			Eentity entity = Eentity.findById(entityId);
			
			user.editOrganizerPermissions(entity, canEdit, canPost, canDelete, canCreateOrganisation, canInvite, canAdministerTopic, canBeRestricted, canAdministerEntity);	
			user.save();
			
			statusMessage="Permissions for user: "+user.getUsername()+" in Entity: "+entity.getName()+" completed succesfully";
			
			
		}
		catch(Exception e){
			statusMessage="Error while editing permissions";
		}
		render(statusMessage);
		
	}
	
	/**
	 * viewRoleInEntity() is used to render a view containing editable permissions of an Organizer in an Entity
	 * 
	 * @author Hossam Saraya
	 * @version May 3, 2011
	 * @category C1S10
	 * @param	entityId: 	Associated entity Id
	 * 			userId:		Involved organizer Id
	 * 
	 * @renders Role Object: 	The role object corresponding to the user,entity
	 * 			User:			The organizer
	 * 			Entity:			The entity
	 */
	public static void viewRoleInEntity(Long entityId,Long userId){
		User user = User.findById(userId);
		Eentity entity = Eentity.findById(entityId);
		
		for (int i = 0; i < user.getRoleList().size(); i++) {
			if(user.getRoleList().get(i).getRelatedEntity().equals(entity) && user.getRoleList().get(i).getTitle().equals("Organizer"))
				render(user.getRoleList().get(i),user,entity);
		}
		
		
	}
	

	
	
}
